package com.example.demo.T2.controller;

import com.example.demo.T2.service.TollService;
import com.example.demo.T2.pojo.Car;
import com.example.demo.T2.pojo.QueueInfo;
import com.example.demo.T2.pojo.TollType;
import com.example.demo.T2.util.RangeStepRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * TestController..
 *
 * @author Lizhong
 * @date 2020/8/18
 */
@Controller
public class TollController {

    @Autowired
    TollService tollService;

    final AtomicBoolean started = new AtomicBoolean(false);

    LocalDateTime startTime = null;

    @GetMapping("/")
    public String toIndex() {


        return "begin";
    }

    @GetMapping("/start")
    public String start(Model model) {

        if (!started.get()) {
            synchronized (this) {
                startTime = LocalDateTime.now();

                test(40);
                started.set(true);
            }
        }
        model.addAttribute("seconds", 0);
        model.addAttribute("fakeTime", "00:00");
        return "sample::resultsList";
    }


    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public String show(Model model) {


        long seconds = 0;
        if (startTime != null)
            seconds = Duration.between(startTime, LocalDateTime.now()).getSeconds();

        QueueInfo queueInfo = tollService.getQueueInfo();

        model.addAttribute("mapList", queueInfo.getTollWindow());
        model.addAttribute("enters", queueInfo.getGlobalWaitingQueue());
        model.addAttribute("leaves", queueInfo.getGlobalDeliverQueue());
        model.addAttribute("seconds", seconds);
        model.addAttribute("fakeTime", tollService.fakeTimer.getFakeClock());

        return "sample::resultsList";
    }

    @RequestMapping(value = "/stop", method = RequestMethod.GET)
    public String stop(Model model) {
        synchronized (this) {
            tollService.clear();
            startTime = null;
            started.set(false);
        }
        model.addAttribute("seconds", 0);
        model.addAttribute("fakeTime", "00:00");
        return "sample::resultsList";
    }


    ExecutorService executorService = Executors.newFixedThreadPool(1, Executors.defaultThreadFactory());

    /**
     * 线程执行方法
     */
    public void test(int carNum) {
        // 停顿时间
        RangeStepRandom rangeStepRandom =new RangeStepRandom(40,0,60,1);
        tollService.fakeTimer.reset();

        executorService.submit(() -> {
            int i = 0;
           int sum =0;
            Random random = new Random();
            while (i < carNum && started.get()) {
                try {
                    Car car = new Car();
                    int i1 = i++;
                    car.setName("Car" + i1);
                    car.setCarType(TollType.ETC);
                    // 随机手工车辆
                    if (random.nextInt(10) % 4 == 0) {
                        car.setCarType(TollType.MANUAL);
                    }
                    double next = rangeStepRandom.getNext02();
                    sum+=next;
                    double l = tollService.fakeTimer.fake2Real((long)(next*1000 *60));
                    Thread.sleep( (long)l);
                    tollService.service(car);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 结束设置标记位
            started.set(false);

        });

    }
}
